package com.micro.ai.gateway.controller;

import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.gateway.filter.IpFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * IP黑白名单管理接口
 * 提供动态管理IP黑白名单的API
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/actuator/ip-management")
public class IpManagementController {

    @Autowired
    private IpFilter ipFilter;

    /**
     * 添加IP到黑名单
     */
    @PostMapping("/blacklist/{ip}")
    public Mono<ApiResponse<Boolean>> addToBlacklist(@PathVariable String ip) {
        log.info("添加IP到黑名单：{}", ip);
        return ipFilter.addToBlacklist(ip)
                .map(result -> ApiResponse.success(result, "IP已添加到黑名单"));
    }

    /**
     * 从黑名单移除IP
     */
    @DeleteMapping("/blacklist/{ip}")
    public Mono<ApiResponse<Boolean>> removeFromBlacklist(@PathVariable String ip) {
        log.info("从黑名单移除IP：{}", ip);
        return ipFilter.removeFromBlacklist(ip)
                .map(result -> ApiResponse.success(result, "IP已从黑名单移除"));
    }

    /**
     * 添加IP到白名单
     */
    @PostMapping("/whitelist/{ip}")
    public Mono<ApiResponse<Boolean>> addToWhitelist(@PathVariable String ip) {
        log.info("添加IP到白名单：{}", ip);
        return ipFilter.addToWhitelist(ip)
                .map(result -> ApiResponse.success(result, "IP已添加到白名单"));
    }

    /**
     * 从白名单移除IP
     */
    @DeleteMapping("/whitelist/{ip}")
    public Mono<ApiResponse<Boolean>> removeFromWhitelist(@PathVariable String ip) {
        log.info("从白名单移除IP：{}", ip);
        return ipFilter.removeFromWhitelist(ip)
                .map(result -> ApiResponse.success(result, "IP已从白名单移除"));
    }
}

